package com.bulletelc.apollopre.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;

import com.bulletelc.apollopre.MyApplication;

/**
 * author: lhl
 * time  : 2017/10/19
 * desc  : APP相关信息工具类
 */

public final class AppUtil {

    private AppUtil() {
        //empty
    }

    /**
     * 获取版本号
     *
     * @param context
     * @return 获取失败返回-1
     */
    public static int getVersionCode(Context context) {
        int versionCode = -1;

        try {
            versionCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return versionCode;

    }

    /**
     * 获取版本名称
     *
     * @param context
     * @return 获取失败返回null
     */
    public static String getVersionName(Context context) {
        String versionName = null;
        try {
            versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    /**
     * 判断app是否安装
     *
     * @param packageName 包名
     * @return
     */
    public static boolean isInstallApp(String packageName) {
        return !isSpace(packageName) && IntentUtil.getLaunchAppIntent(packageName) != null;
    }

    /**
     * 安装APP (支持6.0)
     * @param context
     * @param filePath  apk安装包路径
     */
    public static void installApp(Context context,String filePath) {
        context.startActivity(IntentUtil.getInstallAppIntent(filePath));
    }

    /**
     * 安装App (支持6.0)
     * @param activity
     * @param filePath   apk安装包路径
     * @param requestCode  请求码
     */
    public static void installApp(Activity activity, String filePath, int requestCode) {
        activity.startActivityForResult(IntentUtil.getInstallAppIntent(filePath),requestCode);
    }


    /**
     * 卸载 App
     * @param context   上下文对象
     * @param packageName  包名
     */
    public static void uninstallApp(Context context,String packageName) {
        if (isSpace(packageName)) return;
        context.startActivity(IntentUtil.getUninstallAppIntent(packageName));
    }

    /**
     * 卸载 App
     * @param activity
     * @param packageName  包名
     * @param requestCode  请求码
     */
    public static void uninstallApp(Activity activity, String packageName,int requestCode) {
        if (isSpace(packageName)) return;
        activity.startActivityForResult(IntentUtil.getUninstallAppIntent(packageName),requestCode);
    }

    /**
     *  打开 App
     * @param packageName
     */
    public static void launchApp(String packageName) {
        if (isSpace(packageName)) return;
        MyApplication.getInstance().startActivity(IntentUtil.getLaunchAppIntent(packageName));
    }

    /**
     * 打开 App
     * @param activity
     * @param packageName  包名
     * @param requestCode  请求码
     */
    public static void launchApp(Activity activity, String packageName, int requestCode) {
        if (isSpace(packageName))return;
        activity.startActivityForResult(IntentUtil.getLaunchAppIntent(packageName),requestCode);
    }

    /**
     * 获取App的具体设置
     * @param context
     */
    public static void getAppDetailsSettings(Context context) {
        context.startActivity(IntentUtil.getAppDetailsSettingsIntent(context.getPackageName()));
    }




    private static boolean isSpace(String s) {
        if (s == null) return true;
        for (int i = 0, len = s.length(); i < len; ++i) {
            if (!Character.isWhitespace(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }

}
